package io.github.rascaler.dcenter.pojo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName Candlestick
 * @Description TODO
 * @Author wurenqing
 * @Date 2023-11-23 11:06
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Candlestick {
    //                1499040000000,      // Open time
//                "0.01634790",       // Open
//                "0.80000000",       // High
//                "0.01575800",       // Low
//                "0.01577100",       // Close
//                "148976.11427815",  // Volume
    private Long barDate;
    private BigDecimal open;
    private BigDecimal high;
    private BigDecimal low;
    private BigDecimal close;
    private BigDecimal volume;
    private BigDecimal amount;

//    public Date getDt() {
//        return new Date(barDate);
//    }

//    public static Candlestick parseBinanceObject(String kstr) {
//        JSONArray e = JSON.parseArray(kstr);
//        Candlestick candlestick = new Candlestick();
//        candlestick.setBarDate(e.getLong(0));
//        candlestick.setOpen(e.getBigDecimal(1));
//        candlestick.setHigh(e.getBigDecimal(2));
//        candlestick.setLow(e.getBigDecimal(3));
//        candlestick.setClose(e.getBigDecimal(4));
//        candlestick.setVolume(e.getBigDecimal(5));
//        candlestick.setAmount(new BigDecimal(0));
//        return candlestick;
//    }
//
//    public static List<Candlestick> parseBinanceArray(String kstr) {
//        List<JSONArray> list = JSON.parseArray(kstr, JSONArray.class);
//        List<Candlestick> result = list.stream().map(e -> {
//            Candlestick candlestick = new Candlestick();
//            candlestick.setBarDate(e.getLong(0));
//            candlestick.setOpen(e.getBigDecimal(1));
//            candlestick.setHigh(e.getBigDecimal(2));
//            candlestick.setLow(e.getBigDecimal(3));
//            candlestick.setClose(e.getBigDecimal(4));
//            candlestick.setVolume(e.getBigDecimal(5));
//            candlestick.setAmount(new BigDecimal(0));
//            return candlestick;
//        }).collect(Collectors.toList());
//        return result;
//    }
//
    public String toCsv(String key, String split) {
        StringBuilder builder = new StringBuilder();
        builder.append(key).append(split)
                .append(barDate).append(split)
                .append(open).append(split)
                .append(high).append(split)
                .append(low).append(split)
                .append(close).append(split)
                .append(volume).append(split)
                .append(amount).append("\n");
        return builder.toString();
    }

    public static Object[] parseCsv(String line, String split) {
        Candlestick candlestick = new Candlestick();
        String[] val = line.split(split);
        candlestick.setBarDate(Long.parseLong(val[1]));
        candlestick.setOpen(new BigDecimal(val[2]));
        candlestick.setHigh(new BigDecimal(val[3]));
        candlestick.setLow(new BigDecimal(val[4]));
        candlestick.setClose(new BigDecimal(val[5]));
        candlestick.setVolume(new BigDecimal(val[6]));
        candlestick.setAmount(new BigDecimal(val[7]));
        return new Object[]{val[0], candlestick};
    }



}